//
//  LBLabel+Router.m
//  LBRouter
//
//  Created by WuShiHai on 19/5/24.
//  Copyright (c) 2019 Lebbay. All rights reserved.
//

#import "LBLabel+Router.h"
#import <objc/runtime.h>
#import "LBRouter.h"

@interface LBLabel()

@property (nonatomic, strong) LBRouterAction *routerAction;

@end

@implementation LBLabel (Router)

#pragma mark - Action
- (void)addRouterCallbackAction:(NSString *)callbackType
                 complteHandler:(LBRouterActionCallbackBlock)complteHandler{
    [self.activeAction addRouterCallbackAction:callbackType
                                complteHandler:complteHandler];
}

- (void)addDecideHandler:(LBRouterHandleDecideBlock)decideHandler{
    [self.activeAction addDecideHandler:decideHandler];
}

- (void)wd_openLink:(NSString *)router key:(NSString *)key {
    LBRouterAction *action = self.routerAction;
    BOOL isHandler = action.decideHandler?action.decideHandler(router):YES;
    if (isHandler) {
        [LBRouter openURL:router completion:^(LBRouterType type, NSString *URLString, NSString *paths, NSDictionary *userInfo) {
            [action routerComplete:type
                         URLString:URLString
                             paths:paths
                          userInfo:userInfo];
        }];
    }
}
- (LBRouterAction *)activeAction {
    LBRouterAction *action = self.routerAction;
    if (action) {
        return action;
    }
    self.routerAction = [[LBRouterAction alloc] init];
    return self.routerAction;
}
#pragma mark - Getter & Setter
- (LBRouterAction *)routerAction {
    return objc_getAssociatedObject(self, _cmd);
}
- (void)setRouterAction:(LBRouterAction *)routerAction {
    objc_setAssociatedObject(self, @selector(routerAction), routerAction, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

@end
